package com.mycompany.inventory.viewmodel;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.DependsOn;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zhtml.Messagebox;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Bandbox;
import org.zkoss.zul.Tab;

import com.mycompany.inventory.persistence.CarrierDAO;
import com.mycompany.inventory.persistence.CurrencyDAO;
import com.mycompany.inventory.persistence.InventoryDAO;
import com.mycompany.inventory.persistence.OrderDetailsDAO;
import com.mycompany.inventory.persistence.PaymentMethodDAO;
import com.mycompany.inventory.persistence.PaymentTermDAO;
import com.mycompany.inventory.persistence.ProductDAO;
import com.mycompany.inventory.persistence.ProductInventoryDAO;
import com.mycompany.inventory.persistence.PurchaseDAO;
import com.mycompany.inventory.persistence.SaleInfoDAO;
import com.mycompany.inventory.persistence.TaxSchemeDAO;
import com.mycompany.inventory.persistence.VendorDAO;
import com.mycompany.inventory.pojo.Carrier;
import com.mycompany.inventory.pojo.Contact;
import com.mycompany.inventory.pojo.Currency;
import com.mycompany.inventory.pojo.Inventory;
import com.mycompany.inventory.pojo.OrderDetails;
import com.mycompany.inventory.pojo.PaymentMethod;
import com.mycompany.inventory.pojo.PaymentTerm;
import com.mycompany.inventory.pojo.Product;
import com.mycompany.inventory.pojo.ProductInventory;
import com.mycompany.inventory.pojo.Purchase;
import com.mycompany.inventory.pojo.SaleInfo;
import com.mycompany.inventory.pojo.TaxScheme;
import com.mycompany.inventory.pojo.Vendor;
import com.mycompany.inventory.util.Constant;

public class PurchaseViewModel implements org.zkoss.zk.ui.event.EventListener{
	private String Id;
	private Date purchaseDate;
	private String status;
	private String remark;
	private String orderId;
	private Date datePaid;
	private Date returnDate;
	private double paid;
	private Date reqShipDate;
	private String freight;
	private String subTotal;
	private double bill;
	private int totalOrder;
	private int totalReceived;
	private String remarkReceive;
	private String remarkPayment;
	private String remarkReturn;
	private double refund;
	private String remarkUnstock;
	private double fee;
	private Date dueDate;
	private double totalPayment;
	private double totalReturn;
	private double balance;
	private double credit;
	public double total;
	private String display;
	
	private Vendor selectedVendor;
	private List<Vendor> vendors = new ArrayList<Vendor>();
	
	private PaymentTerm selectedPaymentTerm;
	private List<PaymentTerm> paymentTerms = new ArrayList<PaymentTerm>();
	
	private PaymentMethod selectedPaymentMethod;
	private List<PaymentMethod> paymentMethods = new ArrayList<PaymentMethod>();
	
	private Inventory selectedInventory;
	private List<Inventory> inventory = new ArrayList<Inventory>();
	
	private TaxScheme selectedTaxScheme;
	private List<TaxScheme> tax = new ArrayList<TaxScheme>();
	
	private Currency selectedCurrency;
	private List<Currency> currency = new ArrayList<Currency>();

	//carrier
	private Carrier selectedCarrier;
	private List<Carrier> carrier = new ArrayList();	
	
	private Product selectedPurchaseProduct;
	private List<Product> purchaseProducts = new ArrayList<Product>();

	private Product selectedProduct;
	private List<Product> products = new ArrayList();
	ProductDAO productDAO = new ProductDAO();
	
	private List<SaleInfo> sale = new ArrayList<SaleInfo>();
	private SaleInfoDAO saleDAO = new SaleInfoDAO();	
	
	private Purchase selectedPurchase;
	private List<Purchase> purchases = new ArrayList<Purchase>();
	PurchaseDAO purchaseDAO = new PurchaseDAO();
	private List<OrderDetails> orders = new ArrayList<OrderDetails>();
	private List<OrderDetails> order = new ArrayList<OrderDetails>();
	private boolean isNew = true;
	public Contact contact = new Contact();
	private String fullAddress;
	OrderDetails orderDetail = new OrderDetails();
	OrderDetails objOrderDetail = new OrderDetails();
	
	//Receive
	private Product selectedReceiveProduct;
	private List<Product> receiveProducts = new ArrayList<Product>();
	OrderDetailsDAO dao = new OrderDetailsDAO();
	//Payment
	private Product selectedPaymentProduct;
	private List<Product> paymentProducts = new ArrayList<Product>();
	//Return
	public Product selectedReturnProduct;
	public List<Product> returnedProducts = new ArrayList<Product>();
	//Unstock
	public Product selectedUnstockProduct;
	public List<Product> unstockProducts = new ArrayList<Product>();
	
	public void onEvent(Event evt) throws InterruptedException {
        if (evt.getName().equals("onOK")) {

        } else {	
        	return;
        }
    }

	@Init
	public void init() {
		this.refeshPurchaseGrid();
		this.getInfomation();
	}
	
	public void getInfomation(){
		this.updateVendor();
		
		this.updatePaymentTerm();
		
		this.updateTaxScheme();
		
		this.updateInventory();
		
		this.updateCarrier();
		
		this.updateCurrency();
	}
	
	public void refeshPurchaseGrid(){
		PurchaseDAO dao = new PurchaseDAO();
		List<OrderDetails> lOrders = null;
		List<OrderDetails> oOrders = null;
		this.purchases = dao.findAll();
		double total = 0 ;
		for (Purchase pros : this.purchases) {			
			lOrders = this.dao.getTotalPayment(pros.getOrderId(), Constant.IN_RECEIVE);
			if(lOrders != null && lOrders.size() > 0){
				for(OrderDetails ord : lOrders){
					double c = Double.parseDouble(this.productDAO.getProductCost(Long.parseLong(ord.getProductId())).get(0).getCost()); 
					total += ord.getQuantityReceived()*c - ord.getQuantityReceived()*c*Double.parseDouble(ord.getDiscount())/100;
				}
			}else{
				oOrders = this.dao.getTotalPayment(pros.getOrderId(), Constant.IN_ORDER);
				for(OrderDetails o : oOrders){
					double c = Double.parseDouble(this.productDAO.getProductCost(Long.parseLong(o.getProductId())).get(0).getCost()); 
					total += Integer.parseInt(o.getQuantity())*c - Integer.parseInt(o.getQuantity())*c*Double.parseDouble(o.getDiscount())/100;
				}
			}
			pros.setTotal(total);
			total = 0;
		}
		
		//this.selectedPurchase = (this.purchases == null || this.purchases.size() == 0) ? null : this.purchases.get(0);
	}
	
	@NotifyChange({"selectedVendor","purchaseDate","purchaseProducts","reqShipDate","freight","remark","orderId","products", "contact","selectedPaymentTerm","status","selectedCurrency","selectedTaxScheme","selectedCarrier","subTotal","bill","fullAddress"})
	@Command
	public void selectOrderTab(@BindingParam("para") Component tab){
		Tab t = (Tab)tab;
		if(this.selectedPurchase == null){
			return;
		}

		this.getInfoPurchase();
		t.setSelected(true);
	}
	
	@NotifyChange({"products", "receiveProducts","inventory","selectedInventory","totalOrder","totalReceived","remarkReceive"})
	@Command
	public void selectTabReceive(){
		if(this.selectedPurchase == null){
			return;
		}
		this.getReceivedProducts();
	}

	@NotifyChange({"paymentProducts","totalPayment","paymentMethods","paid","selectedPaymentMethod","remarkPayment","dueDate","datePaid","freight"})	
	@Command
	public void selectTabPayment(){
		if(this.selectedPurchase == null){
			return;
		}
		this.getPaymentedProducts();
	}
	
	@NotifyChange({"returnedProducts","products","returnDate","remarkReturn","fee","refund","totalReturn"})
	@Command
	public void selectTabReturn(@BindingParam("para") Component tab){
		Tab t = (Tab)tab;
		if(this.selectedPurchase == null){
			return;
		}
		this.getReturnProducts();

		t.setSelected(true);
	}
	
	@NotifyChange({"unstockProducts","remarkUnstock"})
	@Command
	public void selectTabUnStock(){
		if(this.selectedPurchase == null){
			return;
		}
		this.getUnstockProduct();	
	}	
	
	@NotifyChange({"products", "purchaseProducts", "carrier", "vendors", "currency", "status", "tax", "inventory", "paymentTerms"})
	@Command
	public void createOrder(@BindingParam("para") Component tab){
		Tab t = (Tab)tab;
		this.purchaseDate = null;
		this.remark = "";
		this.selectedCarrier = null;
		this.selectedVendor = null;
		this.status = Constant.OPEN;
		this.getInfoPurchase();
		t.setSelected(true);
	}	
	
	private void updatePaymentTerm(){
		//init payment term
		PaymentTermDAO dao = new PaymentTermDAO();
		this.paymentTerms = dao.findAll();
		this.selectedPaymentTerm = new PaymentTerm("", "", "", 0);
	}	

	private void updateTaxScheme(){
		//init tax Schemes
		TaxSchemeDAO dao = new TaxSchemeDAO();
		this.tax = dao.findAll();
		this.selectedTaxScheme = new TaxScheme("", "", "", "", 0);
	}
	
	private void updateInventory(){
		//init payment method
		InventoryDAO dao = new InventoryDAO();
		this.inventory = dao.findAll();
		this.selectedInventory= new Inventory("", "", 0);
	}	
	
	private void updateCarrier(){
		//init carrier
		CarrierDAO dao = new CarrierDAO();
		this.carrier = dao.findAll();
		this.selectedCarrier = new Carrier("", "", "");
	}

	private void updateVendor(){
		//init vendor
		VendorDAO vdao = new VendorDAO();
		this.vendors = vdao.findAll();
		this.selectedVendor = new Vendor("", "");
	}
	
	private void updateCurrency(){
		//init vendor
		CurrencyDAO cDao = new CurrencyDAO();
		this.currency = cDao.findAll();
		this.selectedCurrency = new Currency("", "", "");
	}
	
	private void getPaymentMethod(){
		PaymentMethodDAO pmDao = new PaymentMethodDAO();
		this.paymentMethods = pmDao.findAll();
		this.selectedPaymentMethod = new PaymentMethod("", "");
	}
	
	@NotifyChange("purchases")
	@Command("save")
	public void saveOrder(@BindingParam("para") Component tab){
		Purchase purchse;
		PurchaseDAO dao = new PurchaseDAO();
		if(this.selectedPurchase == null){
			purchse = new Purchase("3", this.orderId, this.selectedVendor, this.purchaseDate, Constant.OPEN, this.paid, this.remark, this.datePaid, this.returnDate, this.selectedCurrency, this.selectedTaxScheme, 
						this.selectedPaymentTerm, this.selectedPaymentMethod, this.selectedInventory, this.selectedCarrier, this.reqShipDate, this.freight, this.remarkReceive, this.remarkPayment, this.remarkReturn,
						this.refund, this.remarkUnstock, this.fee, this.dueDate);
			dao.create(purchse);
		}else{
			
		}
		
		this.saveOrders();
		this.refeshPurchaseGrid();
		Tab t = (Tab)tab;
		t.setSelected(true);
	}	

	private void saveOrders(){
		OrderDetailsDAO dao = new OrderDetailsDAO();
		for(Product product : this.purchaseProducts){
			if(!product.getId().equals("")){
				if(this.selectedPurchase != null && dao.findByProductId(Long.parseLong(product.getId()),this.selectedPurchase.getOrderId(),Integer.parseInt(Constant.IN_ORDER)).size()!=0){
					this.orderDetail.setOrderId(this.selectedPurchase.getOrderId());
					this.orderDetail.setProductId(product.getId());
					this.orderDetail.setQuantity(product.getQuantity());
					this.orderDetail.setDiscount(product.getDiscount());
					dao.update(this.orderDetail);
				}else{
					this.orderDetail.setOrderId(this.orderId);
					this.orderDetail.setProductId(product.getId());
					this.orderDetail.setQuantity(product.getQuantity());
					this.orderDetail.setDiscount(product.getDiscount());
					dao.create(this.orderDetail);
				}
			}
		}
	}
	
	@NotifyChange({"bill","selectedVendor","purchaseDate","purchaseProducts","reqShipDate","freight","remark","orderId","products", "contact","selectedPaymentTerm","status","selectedCurrency","selectedTaxScheme","selectedCarrier","fullAddress"})
	@Command
	public void editOrder(@BindingParam("para") Component tab) {
		Tab t = (Tab)tab;
		if(this.selectedPurchase == null){
			Messagebox.show("Please select a order!", "Information", Messagebox.OK, Messagebox.INFORMATION);
			return;
		}
		this.getInfoPurchase();
		t.setSelected(true);
	}	
	
	@NotifyChange({"products","bill"})
	@Command
	public void addProductToOrder(@BindingParam("para") Component comp, @BindingParam("pId")String pId) {
		// manipulate data		
		Component parent = comp.getParent();
		parent = parent.getParent();
		parent = parent.getParent();
		Bandbox box = (Bandbox)parent;
		updateOrderProduct(pId);
		box.close();		 	
	}
	
	private void updateOrderProduct(String id){
		int bill = 0;
		for(Product product : this.purchaseProducts){
			if(product.getId().equals(id)){
				String oQuantity = this.productDAO.getOrderNumber(Long.parseLong(this.selectedProduct.getId()));
				if(oQuantity.equals("")){
					oQuantity = "0";
				}
				int total = Integer.parseInt(oQuantity)*Integer.parseInt(this.selectedProduct.getCost());
				bill+=total; 
				product.copyPurchaseValue(this.selectedProduct);
				product.setQuantity(oQuantity);
				//product.setSubTotal(total);
				break;
			}
		}
		this.bill = bill;
		if(!purchaseProducts.get(purchaseProducts.size() - 1).getId().equals(""))
			purchaseProducts.add(new Product("","","","","","",""));
	}

	private void getInfoPurchase(){
		this.products = this.productDAO.findAll();
		double totalBill = 0;
		for(Product pros : this.products){
			this.sale = this.saleDAO.findbyProductId(Long.parseLong(pros.getId()));
			if(this.sale.size() != 0){
				pros.setNormalPrice(this.sale.get(0).getSaleValue());
			}	
		}
		
		OrderDetailsDAO oDao = new OrderDetailsDAO();
		if(this.selectedPurchase != null){
			this.purchaseProducts = this.productDAO.findProductPurchaseByOrderID(Long.parseLong(this.selectedPurchase.getOrderId()), Long.parseLong(Constant.IN_ORDER));

			for(Product product : this.purchaseProducts){
				this.orders = oDao.findByProductId(Long.parseLong(product.getId()),this.selectedPurchase.getOrderId(),Integer.parseInt(Constant.IN_ORDER));
				double cost = Double.parseDouble(product.getCost());
				double discount = Double.parseDouble(this.orders.get(0).getDiscount());
				int q = Integer.parseInt(this.orders.get(0).getQuantity());				
				double total = cost*q - ((discount/100)*cost*q);
				totalBill += total; 
				product.setQuantity(this.orders.get(0).getQuantity());
				product.setDiscount(this.orders.get(0).getDiscount());
				product.setSubTotal(total);
			}
			this.bill = totalBill;
			if(this.vendors!= null && this.selectedPurchase.getVendor() != null)
				for (int i = 0 ; i < this.vendors.size() ;i++){
					if(this.vendors.get(i).getName().equals(this.selectedPurchase.getVendor().getName())){
						this.selectedVendor = this.vendors.get(i);
						break;
					}
				}
			
			if(this.selectedVendor != null){
				this.contact = this.selectedVendor.getContact();
				if(this.selectedVendor.getAddresses().size() > 0){
					this.fullAddress = this.selectedVendor.getAddresses().get(this.selectedVendor.getAddresses().size() - 1).getFullAddress();
				}				
			}
			
			if(this.paymentTerms!= null && this.selectedPurchase.getPaymentTerm() != null)
				for (int i = 0 ; i < this.paymentTerms.size() ;i++){
					if(this.paymentTerms.get(i).getName().equals(this.selectedPurchase.getPaymentTerm().getName())){
						this.selectedPaymentTerm = this.paymentTerms.get(i);
						break;
					}
				}			
			
			if(this.tax!= null && this.selectedPurchase.getTaxScheme() != null)
				for (int i = 0 ; i < this.tax.size() ;i++){
					if(this.tax.get(i).getName().equals(this.selectedPurchase.getTaxScheme().getName())){
						this.selectedTaxScheme = this.tax.get(i);
						break;
					}
				}
			
			if(this.carrier!= null && this.selectedPurchase.getCarrier() != null)
				for (int i = 0 ; i < this.carrier.size() ;i++){
					if(this.carrier.get(i).getName().equals(this.selectedPurchase.getCarrier().getName())){
						this.selectedCarrier = this.carrier.get(i);
						break;
					}
				}
			
			if(this.currency!= null && this.selectedPurchase.getCurrency() != null)
				for (int i = 0 ; i < this.currency.size() ;i++){
					if(this.currency.get(i).getName().equals(this.selectedPurchase.getCurrency().getName())){
						this.selectedCurrency = this.currency.get(i);
						break;
					}
				}			
			
			this.orderId = this.selectedPurchase.getOrderId();
			this.purchaseDate = this.selectedPurchase.getPurchaseDate();
			this.remark = this.selectedPurchase.getRemark();
			this.reqShipDate = this.selectedPurchase.getReqShipDate();
			if(this.selectedPurchase.getStatus().equals(Constant.OPEN)){
				this.display = Constant.YES;
			}
			this.status = this.selectedPurchase.getStatus();
			this.freight = this.selectedPurchase.getFreight();
			
		}else{
			this.purchaseProducts = new ArrayList<Product>();
		}
		this.purchaseProducts.add(new Product("","","","","","",""));

	}
	
	@NotifyChange({"selectedPaymentTerm","contact","fullAddress","selectedTaxScheme","selectedCurrency","selectedCarrier"})
	@Command
	public void getVendorInfo() {
		this.selectedPaymentTerm = this.selectedVendor.getPaymentTerm();
		this.contact = this.selectedVendor.getContact();
		if(this.selectedVendor.getAddresses().size() > 0){
			this.fullAddress = this.selectedVendor.getAddresses().get(this.selectedVendor.getAddresses().size() - 1).getFullAddress();
		}else{
			this.fullAddress ="";
		}
		
		if(this.paymentTerms!= null && this.selectedVendor.getPaymentTerm() != null)
			for (int i = 0 ; i < this.paymentTerms.size() ;i++){
				if(this.paymentTerms.get(i).getName().equals(this.selectedVendor.getPaymentTerm().getName())){
					this.selectedPaymentTerm = this.paymentTerms.get(i);
					break;
				}
			}			
		
		if(this.tax!= null && this.selectedVendor.getTaxingScheme() != null)
			for (int i = 0 ; i < this.tax.size() ;i++){
				if(this.tax.get(i).getName().equals(this.selectedVendor.getTaxingScheme().getName())){
					this.selectedTaxScheme = this.tax.get(i);
					break;
				}
			}
		
		if(this.carrier!= null && this.selectedVendor.getCarrier() != null)
			for (int i = 0 ; i < this.carrier.size() ;i++){
				if(this.carrier.get(i).getName().equals(this.selectedVendor.getCarrier().getName())){
					this.selectedCarrier = this.carrier.get(i);
					break;
				}
			}
		
		if(this.currency!= null && this.selectedVendor.getCurrency() != null)
			for (int i = 0 ; i < this.currency.size() ;i++){
				if(this.currency.get(i).getName().equals(this.selectedVendor.getCurrency().getName())){
					this.selectedCurrency = this.currency.get(i);
					break;
				}
			}		
		
	}	
	
	//Get info receive
	private void getReceivedProducts(){
		this.products = this.productDAO.findAll();
		int totalO = 0;
		int totalR = 0;
		for(Product pros : this.products){
			this.sale = this.saleDAO.findbyProductId(Long.parseLong(pros.getId()));
			if(this.sale.size() != 0){
				pros.setNormalPrice(this.sale.get(0).getSaleValue());
			}	
		}
		
		if(this.selectedPurchase != null){
			this.remarkReceive = this.selectedPurchase.getRemarkReceive();
			OrderDetailsDAO dao = new OrderDetailsDAO();
			InventoryDAO idao = new InventoryDAO();
			this.receiveProducts = this.productDAO.findProductPurchaseByOrderID(Long.parseLong(this.selectedPurchase.getOrderId()), Long.parseLong(Constant.IN_RECEIVE));
			
			this.purchaseProducts = this.productDAO.findProductPurchaseByOrderID(Long.parseLong(this.selectedPurchase.getOrderId()), Long.parseLong(Constant.IN_ORDER));
				for(Product pros : this.purchaseProducts){
					this.order = dao.findByProductId(Long.parseLong(pros.getId()),this.selectedPurchase.getOrderId(),Integer.parseInt(Constant.IN_ORDER));
					totalO += Integer.parseInt(this.order.get(0).getQuantity());
				}

			for(Product product : this.receiveProducts){
				this.orders = dao.findByProductId(Long.parseLong(product.getId()),this.selectedPurchase.getOrderId(),Integer.parseInt(Constant.IN_RECEIVE));
				product.setQuantity(this.orders.get(0).getQuantity());
				product.setDiscount(this.orders.get(0).getDiscount());
				product.setQuantityReceived(this.orders.get(0).getQuantityReceived());
				product.setReceivedDate(this.orders.get(0).getReceivedDate());
				Inventory ainven = idao.find(Long.parseLong(this.orders.get(0).getLocationId()));
				if(this.inventory!= null && receiveProducts.get(0).getInventory() != null)
					for (int i = 0 ; i < inventory.size() ;i++){
						if(inventory.get(i).getName().equals(ainven.getName())){
							product.setLocation(inventory.get(i));
							break;
						}
					}
				product.setOnOrder(this.orders.get(0).getOnOrder());
				product.setReceiveStatus(this.orders.get(0).getReceiveStatus());
				totalR += this.orders.get(0).getQuantityReceived();
			}			
			this.totalReceived = totalR;
			this.totalOrder = totalO;
			
		}else{
			this.receiveProducts = new ArrayList<Product>();
		}		
			this.receiveProducts.add(new Product("","","","","",null,null,"",0));
	}
	
	//Get info payment
	private void getPaymentedProducts(){
		List<OrderDetails> ordersOnOrder = new ArrayList<OrderDetails>();
		this.products = this.productDAO.findAll();
		for(Product pros : this.products){
			this.sale = this.saleDAO.findbyProductId(Long.parseLong(pros.getId()));
			if(this.sale.size() != 0){
				pros.setNormalPrice(this.sale.get(0).getSaleValue());
			}	
		}
		
		if(this.selectedPurchase != null){
			this.remarkPayment = this.selectedPurchase.getRemarkPayment();
			this.paid = this.selectedPurchase.getPaid();
			this.dueDate = this.selectedPurchase.getDueDate();
			this.datePaid = this.selectedPurchase.getDatePaid();
			this.freight = this.selectedPurchase.getFreight();
			OrderDetailsDAO dao = new OrderDetailsDAO();
			this.paymentProducts = this.productDAO.findProductPurchaseByOrderID(Long.parseLong(this.selectedPurchase.getOrderId()), Long.parseLong(Constant.IN_RECEIVE));
			float subtotal = 0;
			for(Product product : this.paymentProducts){
				this.orders = dao.findByProductId(Long.parseLong(product.getId()),this.selectedPurchase.getOrderId(),Integer.parseInt(Constant.IN_RECEIVE));
				ordersOnOrder = dao.findByProductId(Long.parseLong(product.getId()),this.selectedPurchase.getOrderId(),Integer.parseInt(Constant.IN_ORDER));
				double price = Double.parseDouble(product.getCost());
				double discount = Double.parseDouble((ordersOnOrder.get(0).getDiscount()));
				int quantity = this.orders.get(0).getQuantityReceived();
				double total = price*quantity - ((discount/100)*price*quantity);
				subtotal += total;
				product.setDiscount(ordersOnOrder.get(0).getDiscount());
				product.setQuantityReceived(this.orders.get(0).getQuantityReceived());
				product.setSubTotalPayment(total);				
			}
			this.totalPayment = subtotal;
		}else{
			this.paymentProducts = new ArrayList<Product>();
		}
		
		this.getPaymentMethod();
		if(this.paymentMethods!= null && this.selectedPurchase.getPaymentMethod() != null)
			for (int i = 0 ; i < this.paymentMethods.size() ;i++){
				if(this.paymentMethods.get(i).getName().equals(this.selectedPurchase.getPaymentMethod().getName())){
					this.selectedPaymentMethod = this.paymentMethods.get(i);
					break;
				}
			}
	}	
	
	@NotifyChange({"products"})
	@Command
	public void addProductToReceive(@BindingParam("para") Component comp, @BindingParam("pId")String pId) {
		// manipulate data		
		Component parent = comp.getParent();
		parent = parent.getParent();
		parent = parent.getParent();
		Bandbox box = (Bandbox)parent;
		updateReceiveProduct(pId);
		box.close();		 	
	}	
	
	private void updateReceiveProduct(String id){
		for(Product product : this.receiveProducts){
			if(product.getId().equals(id)){
				String oQuantity = this.productDAO.getOrderNumber(Long.parseLong(this.selectedProduct.getId()));
				if(oQuantity.equals("")){
					oQuantity = "0";
				}
 
				product.copyReceiveProduct(this.selectedProduct);
				product.setQuantityReceived(Integer.parseInt(oQuantity));
				if(this.inventory!= null && this.selectedProduct.getLocation() != null)
					for (int i = 0 ; i < inventory.size() ;i++){
						if(inventory.get(i).getName().equals(this.selectedProduct.getLocation().getName())){
							product.setLocation(inventory.get(i));
							break;
						}
					}
				break;
			}
		}

		if(!receiveProducts.get(receiveProducts.size() - 1).getId().equals(""))
			receiveProducts.add(new Product("","","","","",null,null,"",0));
	}	

	@NotifyChange({"products"})
	@Command
	public void addProductToUnstock(@BindingParam("para") Component comp, @BindingParam("pId")String pId) {
		// manipulate data		
		Component parent = comp.getParent();
		parent = parent.getParent();
		parent = parent.getParent();
		Bandbox box = (Bandbox)parent;
		updateUnstockProduct(pId);
		box.close();		 	
	}	
	
	private void updateUnstockProduct(String id){
		for(Product product : this.unstockProducts){
			if(product.getId().equals(id)){
				List<OrderDetails> l = this.dao.getQuantityUnstocks(Long.parseLong(this.selectedProduct.getId()), this.selectedPurchase.getOrderId(), Constant.IN_RETURN, Constant.RETURN);
				int q = 0;
				if(l != null && l.size() > 0){
					q = l.get(0).getQuantityReturn();
				}
				product.copyReceiveProduct(this.selectedProduct);
				product.setQuantityUnstock(q);
				if(this.inventory!= null && this.selectedProduct.getLocation() != null)
					for (int i = 0 ; i < inventory.size() ;i++){
						if(inventory.get(i).getName().equals(this.selectedProduct.getLocation().getName())){
							product.setLocation(inventory.get(i));
							break;
						}
					}
				break;
			}
		}

		if(!unstockProducts.get(unstockProducts.size() - 1).getId().equals(""))
			unstockProducts.add(new Product("","","","","",null,null,"",0));
	}	
	
	@NotifyChange({"products"})
	@Command
	public void addProductToReturn(@BindingParam("para") Component comp, @BindingParam("pId")String pId) {
		// manipulate data		
		Component parent = comp.getParent();
		parent = parent.getParent();
		parent = parent.getParent();
		Bandbox box = (Bandbox)parent;
		updateReturnProduct(pId);
		box.close();		 	
	}
	
	private void updateReturnProduct(String id){

		for(Product product : this.returnedProducts){
			if(product.getId().equals(id)){
				product.copyReturnProduct(this.selectedProduct);
				break;
			}
		}

		if(!returnedProducts.get(returnedProducts.size() - 1).getId().equals(""))
			returnedProducts.add(new Product("","","","","","",""));
	}
	
	@NotifyChange("purchases")
	@Command("saveReceive")
	public void saveOrderReceive(@BindingParam("para") Component tab){

		for(Product product : this.receiveProducts){
			if(!product.getId().equals("")){
				if(this.selectedPurchase != null && dao.findByProductId(Long.parseLong(product.getId()),this.selectedPurchase.getOrderId(),Integer.parseInt(Constant.IN_RECEIVE)).size()!=0){
					this.orderDetail.setOrderId(this.selectedPurchase.getOrderId());
					this.orderDetail.setProductId(product.getId());
					this.orderDetail.setQuantity(product.getQuantity());
					this.orderDetail.setQuantityReceived(product.getQuantityReceived());
					this.orderDetail.setLocationId(product.getLocation().getId());
					this.orderDetail.setReceivedDate(product.getReceivedDate());
					this.orderDetail.setOnOrder(product.getOnOrder());
					dao.updateReceive(this.orderDetail);
				}else{
					this.orderDetail.setOrderId(this.selectedPurchase.getOrderId());
					this.orderDetail.setProductId(product.getId());
					this.orderDetail.setQuantity(product.getQuantity());
					this.orderDetail.setQuantityReceived(product.getQuantityReceived());
					this.orderDetail.setLocationId(product.getLocation().getId());
					this.orderDetail.setReceivedDate(product.getReceivedDate());
					this.orderDetail.setOnOrder(Integer.parseInt(Constant.IN_RECEIVE));
					dao.createReceive(this.orderDetail);
				}
			}
		}
		Purchase p = new Purchase();
		p.setRemarkReceive(this.remarkReceive);
		p.setId(this.selectedPurchase.getId());
		PurchaseDAO pdao = new PurchaseDAO();
		pdao.updateRemarkReceive(p);
		this.refeshPurchaseGrid();
		Tab t = (Tab)tab;
		t.setSelected(true);
	}	
	
	private void updateQuantity(OrderDetails ord){
		ProductInventoryDAO vDao = new ProductInventoryDAO();
		ProductInventory pv = vDao.findInventory(Long.parseLong(ord.getProductId()), Long.parseLong(ord.getLocationId()));
		ProductInventory n = new ProductInventory();
		if(pv != null){
			int t = pv.getQuantity() + ord.getQuantityReceived();
			n.setInventoryId(ord.getLocationId());
			n.setProductId(ord.getProductId());
			n.setQuantity(t);
			vDao.update(n);
		}else{
			n.setInventoryId(ord.getLocationId());
			n.setProductId(ord.getProductId());
			n.setQuantity(ord.getQuantityReceived());
			vDao.create(n);
		}
	}
	
	private void removeQuantity(OrderDetails ord){
		ProductInventoryDAO vDao = new ProductInventoryDAO();
		ProductInventory pv = vDao.findInventory(Long.parseLong(ord.getProductId()), Long.parseLong(ord.getLocationId()));
		ProductInventory n = new ProductInventory();
		if(pv != null){
			int t = pv.getQuantity() - ord.getQuantityUnstock();
			n.setInventoryId(ord.getLocationId());
			n.setProductId(ord.getProductId());
			n.setQuantity(t);
			vDao.update(n);
		}
	}	
	
	@NotifyChange({"receiveProducts","status"})
	@Command("receiveProduct")
	public void changeStatusReceive(@BindingParam("para") Component comp, @BindingParam("pId")String pId){
		OrderDetails od = dao.findByProductId(Long.parseLong(pId),this.selectedPurchase.getOrderId(),Integer.parseInt(Constant.IN_RECEIVE)).get(0);
		if(this.selectedPurchase != null && od != null){
			this.objOrderDetail.setOrderId(this.selectedPurchase.getOrderId());
			this.objOrderDetail.setProductId(pId);
			this.objOrderDetail.setOnOrder(Integer.parseInt(Constant.IN_RECEIVE));
			this.objOrderDetail.setReceiveStatus(Constant.RECEIVED);
			dao.updateStatus(this.objOrderDetail);
			this.updateQuantity(od);
			int count = this.receiveProducts.size();
			List<OrderDetails> ls = dao.getCountReceived(this.selectedPurchase.getOrderId(), Constant.IN_RECEIVE, Constant.RECEIVED);
			Purchase p = new Purchase();
			if(count == ls.size() + 1){
				p.setStatus(Constant.FULL_RECEIVED);
				p.setId(this.selectedPurchase.getId());
				this.purchaseDAO.updateStatus(p);
			}else{
				p.setStatus(Constant.IN_PROGRESS);
				p.setId(this.selectedPurchase.getId());
				this.purchaseDAO.updateStatus(p);
			}
		}
		this.selectTabReceive();
	}
	
	@NotifyChange({"receiveProducts","status"})
	@Command("receiveAll")
	public void receiveAll(){
		for(Product product : this.receiveProducts){
			if(!product.getId().equals("")){
			List<OrderDetails> listOrderDetail = dao.findByProductId(Long.parseLong(product.getId()),this.selectedPurchase.getOrderId(),Integer.parseInt(Constant.IN_RECEIVE));
			OrderDetails od = null;
			if(listOrderDetail != null || listOrderDetail.size() > 0){
				od = listOrderDetail.get(0);
			}else{
				Messagebox.show("You must save before click Complete to receive all products!", "Information", Messagebox.OK, Messagebox.INFORMATION);
				return;
			}
			if(this.selectedPurchase != null && od != null){
				this.objOrderDetail.setOrderId(this.selectedPurchase.getOrderId());
				this.objOrderDetail.setProductId(product.getId());
				this.objOrderDetail.setOnOrder(Integer.parseInt(Constant.IN_RECEIVE));
				this.objOrderDetail.setReceiveStatus(Constant.RECEIVED);
				dao.updateStatus(this.objOrderDetail);
				this.updateQuantity(od);
				int count = this.receiveProducts.size();
				List<OrderDetails> ls = dao.getCountReceived(this.selectedPurchase.getOrderId(), Constant.IN_RECEIVE, Constant.RECEIVED);
				Purchase p = new Purchase();
				if(count == ls.size() + 1){
					p.setStatus(Constant.FULL_RECEIVED);
					p.setId(this.selectedPurchase.getId());
					this.purchaseDAO.updateStatus(p);
				}else{
					p.setStatus(Constant.IN_PROGRESS);
					p.setId(this.selectedPurchase.getId());
					this.purchaseDAO.updateStatus(p);
				}
			}			
		}
		}
		this.selectTabReceive();
	}	
	
	@NotifyChange("unstockProducts")
	@Command("unstocked")
	public void changeStatusUnstock(@BindingParam("para") Component comp, @BindingParam("pId")String pId){
		OrderDetails od = dao.findByProductId(Long.parseLong(pId),this.selectedPurchase.getOrderId(),Integer.parseInt(Constant.IN_UNSTOCK)).get(0);
		if(this.selectedPurchase != null && od != null){
			this.objOrderDetail.setOrderId(this.selectedPurchase.getOrderId());
			this.objOrderDetail.setProductId(pId);
			this.objOrderDetail.setOnOrder(Integer.parseInt(Constant.IN_UNSTOCK));
			this.objOrderDetail.setReceiveStatus(Constant.UNSTOCKED);
			dao.updateStatus(this.objOrderDetail);
			this.removeQuantity(od);
		}
		this.selectTabUnStock();
	}
	
	@NotifyChange("unstockProducts")
	@Command("unstockAll")
	public void unstockAll(){
		for(Product product : this.unstockProducts){
			if(!product.getId().equals("")){
				List<OrderDetails> listOrderDetail = dao.findByProductId(Long.parseLong(product.getId()),this.selectedPurchase.getOrderId(),Integer.parseInt(Constant.IN_UNSTOCK));
				OrderDetails od = null;
				if(listOrderDetail != null || listOrderDetail.size() > 0){
					od = listOrderDetail.get(0);
				}else{
					Messagebox.show("You must save before click Unstock All to return all products!", "Information", Messagebox.OK, Messagebox.INFORMATION);
					return;
				}
				if(this.selectedPurchase != null && od != null){
					this.objOrderDetail.setOrderId(this.selectedPurchase.getOrderId());
					this.objOrderDetail.setProductId(product.getId());
					this.objOrderDetail.setOnOrder(Integer.parseInt(Constant.IN_UNSTOCK));
					this.objOrderDetail.setReceiveStatus(Constant.UNSTOCKED);
					dao.updateStatus(this.objOrderDetail);
					this.removeQuantity(od);
				}
			}
		}
		this.selectTabUnStock();
	}	
	
	@Command("savePayment")
	public void savePayment(){
		if(this.selectedPurchase != null){
			PurchaseDAO dao = new PurchaseDAO();
			Purchase paymentPurchase = new Purchase();
			paymentPurchase.setPaymentMethod(this.selectedPaymentMethod);
			paymentPurchase.setDueDate(this.dueDate);
			paymentPurchase.setDatePaid(this.datePaid);
			paymentPurchase.setRemarkPayment(this.remarkPayment);
			paymentPurchase.setPaid(this.paid);
			paymentPurchase.setId(this.selectedPurchase.getId());
			dao.updatePaymentPurchase(paymentPurchase);			
		}
	}
	
	@NotifyChange({"datePaid","remarkPayment","dueDate","selectedPaymentMethod","paid","status"})
	@Command("paidFull")
	public void paidFull(){
		if(this.selectedPurchase != null){
			PurchaseDAO dao = new PurchaseDAO();
			Purchase paymentPurchase = new Purchase();
			paymentPurchase.setPaymentMethod(this.selectedPaymentMethod);
			paymentPurchase.setDueDate(this.dueDate);
			paymentPurchase.setDatePaid(this.datePaid);
			paymentPurchase.setRemarkPayment(this.remarkPayment);
			paymentPurchase.setPaid(this.totalPayment);
			paymentPurchase.setStatus(Constant.PAID);
			paymentPurchase.setId(this.selectedPurchase.getId());
			dao.updatePaymentPurchase(paymentPurchase);
			dao.updateStatus(paymentPurchase);
		}
	}	
	
	@NotifyChange({"refund","fee","remark_return","return_date"})
	@Command("refundAll")
	public void refundAll(){
		if(this.selectedPurchase != null){
			Purchase returnParchase = new Purchase();
			returnParchase.setRemarkReturn(this.remarkReturn);
			returnParchase.setId(this.selectedPurchase.getId());
			returnParchase.setReturnDate(this.returnDate);
			returnParchase.setFee(this.fee);
			returnParchase.setRefund(this.totalReturn);
			PurchaseDAO pdao = new PurchaseDAO();
			pdao.updateReturnPurchase(returnParchase);
		}
	}	
	
	//Get info return
	private void getReturnProducts(){
		List<OrderDetails> ordersReturns = new ArrayList<OrderDetails>();
		double returnTotal = 0;
		this.products = this.productDAO.findAll();
		for(Product pros : this.products){
			this.sale = this.saleDAO.findbyProductId(Long.parseLong(pros.getId()));
			if(this.sale.size() != 0){
				pros.setNormalPrice(this.sale.get(0).getSaleValue());
			}	
		}		
		
		if(this.selectedPurchase != null){
			this.remarkReturn = this.selectedPurchase.getRemarkReturn();
			this.paid = this.selectedPurchase.getPaid();
			this.returnDate = this.selectedPurchase.getReturnDate();
			this.freight = this.selectedPurchase.getFreight();
			this.fee = this.selectedPurchase.getFee();
			this.refund = this.selectedPurchase.getRefund();
			OrderDetailsDAO dao = new OrderDetailsDAO();
			this.returnedProducts = this.productDAO.findProductPurchaseByOrderID(Long.parseLong(this.selectedPurchase.getOrderId()), Long.parseLong(Constant.IN_RETURN));
			for(Product product : this.returnedProducts){
				this.orders = dao.findByProductId(Long.parseLong(product.getId()),this.selectedPurchase.getOrderId(),Integer.parseInt(Constant.IN_RETURN));
				ordersReturns = dao.findByProductId(Long.parseLong(product.getId()),this.selectedPurchase.getOrderId(),Integer.parseInt(Constant.IN_ORDER));
				double price = Double.parseDouble(product.getCost());
				int quantity = this.orders.get(0).getQuantityReturn();
				double total = price*quantity;
				returnTotal += total;
				product.setQuantityReturn(quantity);
				product.setSubTotalReturn(total);				
			}
			this.totalReturn = returnTotal;
		}else{
			this.returnedProducts = new ArrayList<Product>();
		}
		this.returnedProducts.add(new Product("","","","","","",""));
	}
	
	@Command("saveReturn")
	public void saveReturn(@BindingParam("para") Component tab){
		List<OrderDetails> aOrders = new ArrayList<OrderDetails>();

		for(Product product : this.returnedProducts){
			if(!product.getId().equals("")){
				aOrders = dao.findByProductId(Long.parseLong(product.getId()),this.selectedPurchase.getOrderId(),Integer.parseInt(Constant.IN_RECEIVE));
				if(aOrders.size() != 0 && aOrders.get(0).getReceiveStatus().equals("Y")){
					int quantityReceived = aOrders.get(0).getQuantityReceived();
					if(quantityReceived >= product.getQuantityReturn()){
						if(this.selectedPurchase != null && dao.findByProductId(Long.parseLong(product.getId()),this.selectedPurchase.getOrderId(),Integer.parseInt(Constant.IN_RETURN)).size()!=0){					
							this.orderDetail.setOrderId(this.selectedPurchase.getOrderId());
							this.orderDetail.setProductId(product.getId());
							this.orderDetail.setQuantityReturn(product.getQuantityReturn());
							this.orderDetail.setReceiveStatus(Constant.RETURN);
							this.orderDetail.setOnOrder(Integer.parseInt(Constant.IN_RETURN));
							dao.updateReturn(this.orderDetail);
						}else{
							this.orderDetail.setOrderId(this.selectedPurchase.getOrderId());
							this.orderDetail.setProductId(product.getId());
							this.orderDetail.setQuantityReturn(product.getQuantityReturn());
							this.orderDetail.setReceiveStatus(Constant.RETURN);
							this.orderDetail.setOnOrder(Integer.parseInt(Constant.IN_RETURN));
							dao.createReturn(this.orderDetail);					
						}
					}else{
						Messagebox.show("The quantity return must least than one was received!", "Information", Messagebox.OK, Messagebox.INFORMATION);
						return;						
					}
				}else{
					Messagebox.show("This product have not received yet!", "Information", Messagebox.OK, Messagebox.INFORMATION);
					return;
				}			
			}			
		}
		Purchase returnParchase = new Purchase();
		returnParchase.setRemarkReturn(this.remarkReturn);
		returnParchase.setId(this.selectedPurchase.getId());
		returnParchase.setReturnDate(this.returnDate);
		returnParchase.setFee(this.fee);
		returnParchase.setRefund(this.refund);
		PurchaseDAO pdao = new PurchaseDAO();
		pdao.updateReturnPurchase(returnParchase);
		this.refeshPurchaseGrid();
		Tab t = (Tab)tab;
		t.setSelected(true);
	}
	
	@NotifyChange("totalReturn")
	@Command	
	public void getReturnTotal(){
	  double t = 0;
		for(Product product : this.returnedProducts){
			double price = Double.parseDouble(product.getCost());
			int quantity = product.getQuantityReturn();
			t += price*quantity;
		}
		this.setTotalReturn(t);
	 }

	@NotifyChange("bill")
	@Command	
	public void getBillTotal(){
	  double b = 0;
		for(Product product : this.purchaseProducts){
			double price = Double.parseDouble(product.getCost());
			int quantity = Integer.parseInt(product.getQuantity());
			double discount = Double.parseDouble(product.getDiscount());
			b += price*quantity - ((discount/100)*price*quantity);
		}
		this.setBill(b);
	 }
	
	//Get info unstock
	private void getUnstockProduct(){
		this.products = this.productDAO.findAll();

		for(Product pros : this.products){
			this.sale = this.saleDAO.findbyProductId(Long.parseLong(pros.getId()));
			if(this.sale.size() != 0){
				pros.setNormalPrice(this.sale.get(0).getSaleValue());
			}	
		}
		
		if(this.selectedPurchase != null){
			this.remarkUnstock = this.selectedPurchase.getRemarkUnstock();
			OrderDetailsDAO dao = new OrderDetailsDAO();
			InventoryDAO idao = new InventoryDAO();
			this.unstockProducts = this.productDAO.findProductPurchaseByOrderID(Long.parseLong(this.selectedPurchase.getOrderId()), Long.parseLong(Constant.IN_UNSTOCK));
			
			for(Product product : this.unstockProducts){
				this.orders = dao.findByProductId(Long.parseLong(product.getId()),this.selectedPurchase.getOrderId(),Integer.parseInt(Constant.IN_UNSTOCK));
				product.setQuantityUnstock(this.orders.get(0).getQuantityUnstock());
				Inventory in = idao.find(Long.parseLong(this.orders.get(0).getLocationId()));
				if(this.inventory!= null && in != null)
					for (int i = 0 ; i < inventory.size() ;i++){
						if(inventory.get(i).getName().equals(in.getName())){
							product.setLocation(inventory.get(i));
							break;
						}
					}
				product.setOnOrder(this.orders.get(0).getOnOrder());
				product.setReceiveStatus(this.orders.get(0).getReceiveStatus());
			}			
			
		}else{
			this.unstockProducts = new ArrayList<Product>();
		}		
			this.unstockProducts.add(new Product("","","",0,null,""));
	}
	
	@Command("saveUnstock")
	public void saveUnstock(@BindingParam("para") Component tab){
		List<OrderDetails> aOrders = new ArrayList<OrderDetails>();

		for(Product product : this.unstockProducts){
			if(!product.getId().equals("")){
				aOrders = dao.findByProductId(Long.parseLong(product.getId()),this.selectedPurchase.getOrderId(),Integer.parseInt(Constant.IN_RETURN));
				if(aOrders.size() != 0 && aOrders.get(0).getReceiveStatus().equals("R")){
					int quantityReturn = aOrders.get(0).getQuantityReturn();
					if(quantityReturn >= product.getQuantityUnstock()){
						if(this.selectedPurchase != null && dao.findByProductId(Long.parseLong(product.getId()),this.selectedPurchase.getOrderId(),Integer.parseInt(Constant.IN_UNSTOCK)).size()!=0){					
							this.orderDetail.setOrderId(this.selectedPurchase.getOrderId());
							this.orderDetail.setProductId(product.getId());
							this.orderDetail.setQuantityUnstock(product.getQuantityUnstock());
							this.orderDetail.setLocationId(product.getLocation().getId());
							this.orderDetail.setReceiveStatus(Constant.UNSTOCK);
							this.orderDetail.setOnOrder(Integer.parseInt(Constant.IN_UNSTOCK));
							dao.updateUnstock(this.orderDetail);
						}else{
							this.orderDetail.setOrderId(this.selectedPurchase.getOrderId());
							this.orderDetail.setProductId(product.getId());
							this.orderDetail.setQuantityUnstock(product.getQuantityUnstock());
							this.orderDetail.setLocationId(product.getLocation().getId());
							this.orderDetail.setReceiveStatus(Constant.UNSTOCK);
							this.orderDetail.setOnOrder(Integer.parseInt(Constant.IN_UNSTOCK));
							dao.createUnstock(this.orderDetail);					
						}
					}else{
						Messagebox.show("The quantity unstock must least than one was received!", "Information", Messagebox.OK, Messagebox.INFORMATION);
						return;						
					}
				}else{
					Messagebox.show("This product have not returned yet!", "Information", Messagebox.OK, Messagebox.INFORMATION);
					return;
				}			
			}			
		}
		Purchase uParchase = new Purchase();
		uParchase.setRemarkUnstock(this.remarkUnstock);
		uParchase.setId(this.selectedPurchase.getId());
		PurchaseDAO pdao = new PurchaseDAO();
		pdao.updateUnstockPurchase(uParchase);
		this.refeshPurchaseGrid();
		Tab t = (Tab)tab;
		t.setSelected(true);
	}	
	
	/**
	 * @return the id
	 */
	public String getId() {
		return Id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		Id = id;
	}

	/**
	 * @return the purchaseDate
	 */
	public Date getPurchaseDate() {
		return purchaseDate;
	}

	/**
	 * @param purchaseDate the purchaseDate to set
	 */
	public void setPurchaseDate(Date purchaseDate) {
		this.purchaseDate = purchaseDate;
	}

	/**
	 * @return the status
	 */
	public String getStatus() {
		return status;
	}

	/**
	 * @param status the status to set
	 */
	public void setStatus(String status) {
		this.status = status;
	}

	/**
	 * @return the remark
	 */
	public String getRemark() {
		return remark;
	}

	/**
	 * @param remark the remark to set
	 */
	public void setRemark(String remark) {
		this.remark = remark;
	}

	/**
	 * @return the selectedVendor
	 */
	public Vendor getSelectedVendor() {
		return selectedVendor;
	}

	/**
	 * @param selectedVendor the selectedVendor to set
	 */
	public void setSelectedVendor(Vendor selectedVendor) {
		this.selectedVendor = selectedVendor;
	}

	/**
	 * @return the vendors
	 */
	public List<Vendor> getVendors() {
		return vendors;
	}

	/**
	 * @param vendors the vendors to set
	 */
	public void setVendors(List<Vendor> vendors) {
		this.vendors = vendors;
	}

	/**
	 * @return the selectedPaymentTerm
	 */
	public PaymentTerm getSelectedPaymentTerm() {
		return selectedPaymentTerm;
	}

	/**
	 * @param selectedPaymentTerm the selectedPaymentTerm to set
	 */
	public void setSelectedPaymentTerm(PaymentTerm selectedPaymentTerm) {
		this.selectedPaymentTerm = selectedPaymentTerm;
	}

	/**
	 * @return the paymentTerms
	 */
	public List<PaymentTerm> getPaymentTerms() {
		return paymentTerms;
	}

	/**
	 * @param paymentTerms the paymentTerms to set
	 */
	public void setPaymentTerms(List<PaymentTerm> paymentTerms) {
		this.paymentTerms = paymentTerms;
	}

	/**
	 * @return the selectedInventory
	 */
	public Inventory getSelectedInventory() {
		return selectedInventory;
	}

	/**
	 * @param selectedInventory the selectedInventory to set
	 */
	public void setSelectedInventory(Inventory selectedInventory) {
		this.selectedInventory = selectedInventory;
	}

	/**
	 * @return the inventory
	 */
	public List<Inventory> getInventory() {
		return inventory;
	}

	/**
	 * @param inventory the inventory to set
	 */
	public void setInventory(List<Inventory> inventory) {
		this.inventory = inventory;
	}

	/**
	 * @return the selectedTaxScheme
	 */
	public TaxScheme getSelectedTaxScheme() {
		return selectedTaxScheme;
	}

	/**
	 * @param selectedTaxScheme the selectedTaxScheme to set
	 */
	public void setSelectedTaxScheme(TaxScheme selectedTaxScheme) {
		this.selectedTaxScheme = selectedTaxScheme;
	}

	/**
	 * @return the tax
	 */
	public List<TaxScheme> getTax() {
		return tax;
	}

	/**
	 * @param tax the tax to set
	 */
	public void setTax(List<TaxScheme> tax) {
		this.tax = tax;
	}

	/**
	 * @return the selectedCurrency
	 */
	public Currency getSelectedCurrency() {
		return selectedCurrency;
	}

	/**
	 * @param selectedCurrency the selectedCurrency to set
	 */
	public void setSelectedCurrency(Currency selectedCurrency) {
		this.selectedCurrency = selectedCurrency;
	}

	/**
	 * @return the currency
	 */
	public List<Currency> getCurrency() {
		return currency;
	}

	/**
	 * @param currency the currency to set
	 */
	public void setCurrency(List<Currency> currency) {
		this.currency = currency;
	}

	/**
	 * @return the selectedCarrier
	 */
	public Carrier getSelectedCarrier() {
		return selectedCarrier;
	}

	/**
	 * @param selectedCarrier the selectedCarrier to set
	 */
	public void setSelectedCarrier(Carrier selectedCarrier) {
		this.selectedCarrier = selectedCarrier;
	}

	/**
	 * @return the carrier
	 */
	public List<Carrier> getCarrier() {
		return carrier;
	}

	/**
	 * @param carrier the carrier to set
	 */
	public void setCarrier(List<Carrier> carrier) {
		this.carrier = carrier;
	}

	/**
	 * @return the selectedPurchaseProduct
	 */
	public Product getSelectedPurchaseProduct() {
		return selectedPurchaseProduct;
	}

	/**
	 * @param selectedPurchaseProduct the selectedPurchaseProduct to set
	 */
	public void setSelectedPurchaseProduct(Product selectedPurchaseProduct) {
		this.selectedPurchaseProduct = selectedPurchaseProduct;
	}

	/**
	 * @return the purchaseProducts
	 */
	public List<Product> getPurchaseProducts() {
		return purchaseProducts;
	}

	/**
	 * @param purchaseProducts the purchaseProducts to set
	 */
	public void setPurchaseProducts(List<Product> purchaseProducts) {
		this.purchaseProducts = purchaseProducts;
	}

	/**
	 * @return the selectedPurchase
	 */
	public Purchase getSelectedPurchase() {
		return selectedPurchase;
	}

	/**
	 * @param selectedPurchase the selectedPurchase to set
	 */
	public void setSelectedPurchase(Purchase selectedPurchase) {
		this.selectedPurchase = selectedPurchase;
	}

	/**
	 * @return the purchases
	 */
	public List<Purchase> getPurchases() {
		return purchases;
	}

	/**
	 * @param purchases the purchases to set
	 */
	public void setPurchases(List<Purchase> purchases) {
		this.purchases = purchases;
	}

	/**
	 * @return the isNew
	 */
	public boolean isNew() {
		return isNew;
	}

	/**
	 * @param isNew the isNew to set
	 */
	public void setNew(boolean isNew) {
		this.isNew = isNew;
	}

	/**
	 * @return the selectedProduct
	 */
	public Product getSelectedProduct() {
		return selectedProduct;
	}

	/**
	 * @param selectedProduct the selectedProduct to set
	 */
	public void setSelectedProduct(Product selectedProduct) {
		this.selectedProduct = selectedProduct;
	}

	/**
	 * @param products the products to set
	 */
	public void setProducts(List<Product> products) {
		this.products = products;
	}

	/**
	 * @return the sale
	 */
	public List<SaleInfo> getSale() {
		return sale;
	}

	/**
	 * @param sale the sale to set
	 */
	public void setSale(List<SaleInfo> sale) {
		this.sale = sale;
	}

	/**
	 * @return the orders
	 */
	public List<OrderDetails> getOrders() {
		return orders;
	}

	/**
	 * @param orders the orders to set
	 */
	public void setOrders(List<OrderDetails> orders) {
		this.orders = orders;
	}

	/**
	 * @return the products
	 */
	public List<Product> getProducts() {
		return products;
	}

	/**
	 * @return the subTotal
	 */
	public String getSubTotal() {
		return subTotal;
	}

	/**
	 * @param subTotal the subTotal to set
	 */
	public void setSubTotal(String subTotal) {
		this.subTotal = subTotal;
	}

	/**
	 * @return the contact
	 */
	public Contact getContact() {
		return contact;
	}

	/**
	 * @param contact the contact to set
	 */
	public void setContact(Contact contact) {
		this.contact = contact;
	}

	/**
	 * @return the fullAddress
	 */
	public String getFullAddress() {
		return fullAddress;
	}

	/**
	 * @param fullAddress the fullAddress to set
	 */
	public void setFullAddress(String fullAddress) {
		this.fullAddress = fullAddress;
	}

	/**
	 * @return the orderId
	 */
	public String getOrderId() {
		return orderId;
	}

	/**
	 * @param orderId the orderId to set
	 */
	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	/**
	 * @return the datePaid
	 */
	public Date getDatePaid() {
		return datePaid;
	}

	/**
	 * @param datePaid the datePaid to set
	 */
	public void setDatePaid(Date datePaid) {
		this.datePaid = datePaid;
	}

	/**
	 * @return the returnDate
	 */
	public Date getReturnDate() {
		return returnDate;
	}

	/**
	 * @param returnDate the returnDate to set
	 */
	public void setReturnDate(Date returnDate) {
		this.returnDate = returnDate;
	}

	/**
	 * @return the paid
	 */
	public double getPaid() {
		return paid;
	}

	/**
	 * @param paid the paid to set
	 */
	public void setPaid(double paid) {
		this.paid = paid;
	}

	/**
	 * @return the reqShipDate
	 */
	public Date getReqShipDate() {
		return reqShipDate;
	}

	/**
	 * @param reqShipDate the reqShipDate to set
	 */
	public void setReqShipDate(Date reqShipDate) {
		this.reqShipDate = reqShipDate;
	}

	/**
	 * @return the freight
	 */
	public String getFreight() {
		return freight;
	}

	/**
	 * @param freight the freight to set
	 */
	public void setFreight(String freight) {
		this.freight = freight;
	}

	/**
	 * @return the paymentMethods
	 */
	public List<PaymentMethod> getPaymentMethods() {
		return paymentMethods;
	}

	/**
	 * @param paymentMethods the paymentMethods to set
	 */
	public void setPaymentMethods(List<PaymentMethod> paymentMethods) {
		this.paymentMethods = paymentMethods;
	}

	/**
	 * @return the selectedPaymentMethod
	 */
	public PaymentMethod getSelectedPaymentMethod() {
		return selectedPaymentMethod;
	}

	/**
	 * @param selectedPaymentMethod the selectedPaymentMethod to set
	 */
	public void setSelectedPaymentMethod(PaymentMethod selectedPaymentMethod) {
		this.selectedPaymentMethod = selectedPaymentMethod;
	}

	/**
	 * @return the orderDetail
	 */
	public OrderDetails getOrderDetail() {
		return orderDetail;
	}

	/**
	 * @param orderDetail the orderDetail to set
	 */
	public void setOrderDetail(OrderDetails orderDetail) {
		this.orderDetail = orderDetail;
	}

	/**
	 * @return the bill
	 */
	public double getBill() {
		return bill;
	}

	/**
	 * @param bill the bill to set
	 */
	public void setBill(double bill) {
		this.bill = bill;
	}

	/**
	 * @return the selectedReceiveProduct
	 */
	public Product getSelectedReceiveProduct() {
		return selectedReceiveProduct;
	}

	/**
	 * @param selectedReceiveProduct the selectedReceiveProduct to set
	 */
	public void setSelectedReceiveProduct(Product selectedReceiveProduct) {
		this.selectedReceiveProduct = selectedReceiveProduct;
	}

	/**
	 * @return the receiveProducts
	 */
	public List<Product> getReceiveProducts() {
		return receiveProducts;
	}

	/**
	 * @param receiveProducts the receiveProducts to set
	 */
	public void setReceiveProducts(List<Product> receiveProducts) {
		this.receiveProducts = receiveProducts;
	}

	/**
	 * @return the totalOrder
	 */
	public int getTotalOrder() {
		return totalOrder;
	}

	/**
	 * @param totalOrder the totalOrder to set
	 */
	public void setTotalOrder(int totalOrder) {
		this.totalOrder = totalOrder;
	}

	/**
	 * @return the totalReceived
	 */
	public int getTotalReceived() {
		return totalReceived;
	}

	/**
	 * @param totalReceived the totalReceived to set
	 */
	public void setTotalReceived(int totalReceived) {
		this.totalReceived = totalReceived;
	}

	/**
	 * @return the remarkReceive
	 */
	public String getRemarkReceive() {
		return remarkReceive;
	}

	/**
	 * @param remarkReceive the remarkReceive to set
	 */
	public void setRemarkReceive(String remarkReceive) {
		this.remarkReceive = remarkReceive;
	}

	/**
	 * @return the remarkPayment
	 */
	public String getRemarkPayment() {
		return remarkPayment;
	}

	/**
	 * @param remarkPayment the remarkPayment to set
	 */
	public void setRemarkPayment(String remarkPayment) {
		this.remarkPayment = remarkPayment;
	}

	/**
	 * @return the remarkReturn
	 */
	public String getRemarkReturn() {
		return remarkReturn;
	}

	/**
	 * @param remarkReturn the remarkReturn to set
	 */
	public void setRemarkReturn(String remarkReturn) {
		this.remarkReturn = remarkReturn;
	}

	/**
	 * @return the refund
	 */
	public double getRefund() {
		return refund;
	}

	/**
	 * @param refund the refund to set
	 */
	public void setRefund(double refund) {
		this.refund = refund;
	}

	/**
	 * @return the remarkUnstock
	 */
	public String getRemarkUnstock() {
		return remarkUnstock;
	}

	/**
	 * @param remarkUnstock the remarkUnstock to set
	 */
	public void setRemarkUnstock(String remarkUnstock) {
		this.remarkUnstock = remarkUnstock;
	}

	/**
	 * @return the fee
	 */
	public double getFee() {
		return fee;
	}

	/**
	 * @param fee the fee to set
	 */
	public void setFee(double fee) {
		this.fee = fee;
	}

	/**
	 * @return the dueDate
	 */
	public Date getDueDate() {
		return dueDate;
	}

	/**
	 * @param dueDate the dueDate to set
	 */
	public void setDueDate(Date dueDate) {
		this.dueDate = dueDate;
	}

	/**
	 * @return the selectedPaymentProduct
	 */
	public Product getSelectedPaymentProduct() {
		return selectedPaymentProduct;
	}

	/**
	 * @param selectedPaymentProduct the selectedPaymentProduct to set
	 */
	public void setSelectedPaymentProduct(Product selectedPaymentProduct) {
		this.selectedPaymentProduct = selectedPaymentProduct;
	}

	/**
	 * @return the paymentProducts
	 */
	public List<Product> getPaymentProducts() {
		return paymentProducts;
	}

	/**
	 * @param paymentProducts the paymentProducts to set
	 */
	public void setPaymentProducts(List<Product> paymentProducts) {
		this.paymentProducts = paymentProducts;
	}

	/**
	 * @return the totalPayment
	 */
	public double getTotalPayment() {
		return totalPayment;
	}

	/**
	 * @param totalPayment the totalPayment to set
	 */
	public void setTotalPayment(double totalPayment) {
		this.totalPayment = totalPayment;
	}
	
	@DependsOn({"totalPayment","paid"})
	public double getBalance(){
		return totalPayment - paid;
	}

	@NotifyChange("totalReturn")
	@DependsOn({"totalReturn","fee","refund"})
	public double getCredit(){
		return totalReturn - fee - refund;
	}	
	
	/**
	 * @return the totalReturn
	 */
	public double getTotalReturn() {
		return totalReturn;
	}

	/**
	 * @param totalReturn the totalReturn to set
	 */
	public void setTotalReturn(double totalReturn) {
		this.totalReturn = totalReturn;
	}

	/**
	 * @return the selectedReturnProduct
	 */
	public Product getSelectedReturnProduct() {
		return selectedReturnProduct;
	}

	/**
	 * @param selectedReturnProduct the selectedReturnProduct to set
	 */
	public void setSelectedReturnProduct(Product selectedReturnProduct) {
		this.selectedReturnProduct = selectedReturnProduct;
	}

	/**
	 * @return the returnedProducts
	 */
	public List<Product> getReturnedProducts() {
		return returnedProducts;
	}

	/**
	 * @param returnedProducts the returnedProducts to set
	 */
	public void setReturnedProducts(List<Product> returnedProducts) {
		this.returnedProducts = returnedProducts;
	}

	/**
	 * @return the selectedUnstockProduct
	 */
	public Product getSelectedUnstockProduct() {
		return selectedUnstockProduct;
	}

	/**
	 * @param selectedUnstockProduct the selectedUnstockProduct to set
	 */
	public void setSelectedUnstockProduct(Product selectedUnstockProduct) {
		this.selectedUnstockProduct = selectedUnstockProduct;
	}

	/**
	 * @return the unstockProducts
	 */
	public List<Product> getUnstockProducts() {
		return unstockProducts;
	}

	/**
	 * @param unstockProducts the unstockProducts to set
	 */
	public void setUnstockProducts(List<Product> unstockProducts) {
		this.unstockProducts = unstockProducts;
	}

	/**
	 * @return the display
	 */
	public String getDisplay() {
		return display;
	}

	/**
	 * @param display the display to set
	 */
	public void setDisplay(String display) {
		this.display = display;
	}
		
}